package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.repository.DestinationMongoRepository;
import cn.wolfcode.wolf2w.repository.TravelCommentMongoRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    
    @Autowired
    private TravelCommentMongoRepository travelCommentMongoRepository;





    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        // 保存创建时间
        travelComment.setCreateTime(new Date());
        // 获取当前游记回复的 refCommentId (前端必定会带过来一个,就算是空值)
        String refId = travelComment.getRefComment().getId();
        if(StringUtils.hasLength(refId)){
            // 获取到关联评论对象
            TravelComment refComment = this.get(refId);
            // 把关联的评论对象设置到当前评论中
            travelComment.setRefComment(refComment);
            // 设置当前评论的类别为 评论的评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }
        travelCommentMongoRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentMongoRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentMongoRepository.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        return travelCommentMongoRepository.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> list() {
        return travelCommentMongoRepository.findAll();
    }

    @Override
    public List<TravelComment> qeuryByTravelId(String travelId) {
        List<TravelComment> list = travelCommentMongoRepository.findByTravelId(travelId);
        return list;
    }



}
